package com.riche.codefactory.job.cycle;

import com.riche.codefactory.mapper.QuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

/**
 * 增量同步题目到 es
 *
 */
// todo 取消注释开启任务
//@Component
@Slf4j
public class IncSyncQuestionToEs {

    @Resource
    private QuestionMapper questionMapper;

/*    @Resource
    private QuestionEsDao questionEsDao;*/

    /**
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60 * 1000)
    public void run() {
        // 查询近 5 分钟内的数据
        // todo 内次只查询近5分钟内的数据 每一份分钟查一次 好处： 哪怕前4次都执行插入失败了，我只要还在5分钟内，数据库都不会出现丢失的现象
        /*long FIVE_MINUTES = 5 * 60 * 1000L;
        Date fiveMinutesAgoDate = new Date(new Date().getTime() - FIVE_MINUTES);
        List<Question> questionList = questionMapper.listQuestionWithDelete(fiveMinutesAgoDate);
        if (CollUtil.isEmpty(questionList)) {
            log.info("no inc question");
            return;
        }
        List<C2sQuestionEsDTO> c2sQuestionEsDTOList = questionList.stream()
                .map(C2sQuestionEsDTO::objToDto)
                .collect(Collectors.toList());
        final int pageSize = 500;
        int total = c2sQuestionEsDTOList.size();
        log.info("IncSyncQuestionToEs start, total {}", total);
        for (int i = 0; i < total; i += pageSize) {
            int end = Math.min(i + pageSize, total);
            log.info("sync from {} to {}", i, end);
            questionEsDao.saveAll(c2sQuestionEsDTOList.subList(i, end));
        }
        log.info("IncSyncQuestionToEs end, total {}", total);*/
    }
}
